package com.itheima.service.impl;



import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.UserManagerDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserManagerService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author Gallant
 * @create 2020-07-10 14:43
 */
@Service(interfaceClass = UserManagerService.class)
@Transactional
public class UserManagerServiceImpl implements UserManagerService {
    @Autowired
    private UserManagerDao userManagerDao;

    //用户新增
    public void add(User user, Integer[] roleIds) {

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(user.getPassword());
        user.setPassword(encode);
        userManagerDao.add(user);
        this.setUserAndRole(user.getId(),roleIds);
    }

    //分页查询
    public PageResult queryPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        //设置分页
         PageHelper.startPage(currentPage, pageSize);
         //调用dao进行查询
        List<User> userList = userManagerDao.queryPage(queryString);
        PageInfo pageInfo = new PageInfo(userList);
        return new PageResult(pageInfo.getTotal(),pageInfo.getList());
    }

    //删除用户
    public void delete(Integer id) {
        //如果有关联,先删除中间表
        userManagerDao.deleteAssociation(id);
        //再删除user表
        userManagerDao.delete(id);
    }

    //根据id查询用户
    public User findById(Integer id) {
        User user = userManagerDao.findById(id);
        return user;
    }

    //根据用户id查询角色id
    public List<Integer> findRoleIdByUserId(Integer id) {
        return userManagerDao.findRoleIdByUserId(id);
    }

    @Override
    public void edit(User user, Integer[] roleIds) {
        //先把用户列表更新
        userManagerDao.update(user);
        //再把关联表中的role_id删除
        userManagerDao.deleteAssociation(user.getId());
        //再把角色列表更新
        this.setUserAndRole(user.getId(),roleIds);
    }

    // 给中间表添加数据
    public void setUserAndRole(Integer userId,Integer[] roleIds){
        // 然后将检查项的id遍历出来,封装为一个map集合,之后再往中间表中添加
        if (roleIds != null && roleIds.length > 0){
            for (Integer roleId : roleIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("user_id",userId);
                map.put("role_id",roleId);
                userManagerDao.setUserAndRole(map);
            }
        }

    }
}
